<script setup>
import { ref, reactive } from "vue"
import store from '@/store/index'
import { useRouter } from 'vue-router'

const router = useRouter()
const title = ref("欢迎登录")
const formRef = ref()
const form = reactive({
  username: "",
  password: ""
})

const rules = reactive({
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 3, max: 16, message: "用户名仅允许输入3到16个字符", trigger: "blur" }
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, max: 18, message: "密码仅允许输入6到18个字符", trigger: "blur" }
  ]
})

const showPassword = ref(false)
const handleShowPassword = () => {
  const { value } = showPassword
  showPassword.value = !value
}

const handleReset = () => {
  formRef.value.resetFields()
}

const handleSubmit = async () => {
  await formRef.value.validate(async (valid, fields) => {
    if (valid) {
      // 调用user模块下的login方法
      store.dispatch('user/login',form)
    } else {
      console.log("error submit!", fields)
    }
  })
}

const handleVisitor = ()=>{
  store.commit('user/set_userRole','visitor')
  router.push('/')
}
</script>

<template>
  <div class="page page_login w100 h100">
    <DateClock />
    <div class="contanier radius8">
      <h3>{{ title }}</h3>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="72px">
        <el-form-item label="用户名:" prop="username">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item label="密码:" prop="password">
          <el-input
            v-model="form.password"
            :type="showPassword ? 'text' : 'password'"
          >
            <template #suffix>
              <View
                style="
                  width: 1em;
                  height: 1em;
                  margin-right: 8px;
                  cursor: pointer;
                "
                @mousedown="handleShowPassword"
                @mouseup="handleShowPassword"
              />
            </template>
          </el-input>
        </el-form-item>
      </el-form>
      <div class="btn_group">
        <el-button @click="handleVisitor"> 访客 </el-button>
        <el-button @click="handleReset"> 重置 </el-button>
        <el-button type="primary" @click="handleSubmit"> 登录 </el-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "Login"
}
</script>

<style lang="scss" scoped>
.page_login {
  position: relative;
  background-image: url("@/assets/images/bg_login_light.jpg");
  background-repeat: no-repeat;
  background-size: cover;
  .contanier {
    position: absolute;
    top: 55%;
    left: 85%;
    transform: translate(-50%, -50%);
    width: 300px;
    height: 300px;
    box-shadow: 0 0 10px 5px $shadow;
    text-align: center;
    background: rgba($color: $primary_bg, $alpha: 0.8);
    h3 {
      line-height: 64px;
      font-size: 24px;
    }
    .el-form {
      margin-bottom: 55px;
      .el-form-item {
        width: 240px;
        margin: 24px auto;
      }
    }
  }
}
</style>
